package com.bsdsma.fengheguaiapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.bsdsma.fengheguaiapp.dto.*;
import com.bsdsma.fengheguaiapp.entity.ServiceInfo;
import com.bsdsma.fengheguaiapp.enums.Role;
import com.bsdsma.fengheguaiapp.mapper.ServiceInfoMapper;
import com.bsdsma.fengheguaiapp.service.ServiceInfoService;
import com.bsdsma.fengheguaiapp.constants.ErrorCodes;
import com.bsdsma.fengheguaiapp.exceptionhandler.GlobalRuntimeException;
import com.bsdsma.fengheguaiapp.utils.CustomerBeanUtils;
import com.bsdsma.fengheguaiapp.utils.DateUtils;
import com.bsdsma.fengheguaiapp.utils.PageRetUtils;
import com.bsdsma.fengheguaiapp.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ServiceInfoServiceImpl implements ServiceInfoService {

    /**
     * 使用Spring注入Mybatis操作对象
     */
    @Autowired
    private ServiceInfoMapper mapper;

    @Override
    public PageRetDto<ServiceInfo> search(ServiceInfoSearchDto dto) {
        ServiceInfo bean = new ServiceInfo();
        bean.setStatus(0);
        QueryWrapper<ServiceInfo> wrapper = new QueryWrapper<>(bean);

        if (!StringUtils.isBlank(dto.getRoomType())) {
            wrapper.like("room_type", "%" + dto.getRoomType() + "%");
        }
        if (!StringUtils.isBlank(dto.getRoomId())) {
            wrapper.like("room_id", "%" + dto.getRoomId() + "%");
        }
        if (!StringUtils.isBlank(dto.getCustomerName())) {
            wrapper.like("customer_name", "%" + dto.getCustomerName() + "%");
        }
        IPage<ServiceInfo> pageData = mapper.selectPage(new PageDTO<>(dto.getPage(), dto.getSize()), wrapper);
        return PageRetUtils.parse(pageData);
    }

    /**
     * 获取房间类型
     * @return
     */
    @Override
    public List<RoomTypeDto> getRoomTypes() {
        return mapper.selectRoomTypes();
    }

    @Override
    public List<ServiceInfo> listAll() {
        ServiceInfo bean = new ServiceInfo();
        bean.setStatus(0);
        return mapper.selectList(new QueryWrapper<>(bean));
    }

    @Override
    public ServiceInfo add(ServiceInfoAddDto dto) {
        ServiceInfo bean = CustomerBeanUtils.copyPropertiesClass(dto, ServiceInfo.class);
        String roomType = dto.getRoomType();
        String roomId = dto.getRoomId();
        String customerName = dto.getCustomerName();
        String orderTime = dto.getOrderTime();
        String note = dto.getNote();

        if (StringUtils.isBlank(roomType)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间类型");
        }
        if (StringUtils.isBlank(roomId)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间号");
        }
        if (StringUtils.isBlank(customerName)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写入住人员");
        }
        if (StringUtils.isBlank(orderTime)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请选择入住时间");
        }
        bean.setOrderTime(DateUtils.parse(orderTime, "yyyy-MM-dd"));
        if (StringUtils.isBlank(note)) {
            bean.setNote("");
        }
        bean.setStatus(0);
        bean.setCt(DateUtils.getNow());
        bean.setUt(DateUtils.getNow());
        mapper.insert(bean);
        return bean;
    }

    @Override
    public ServiceInfo update(Long id, ServiceInfoAddDto dto) {
        ServiceInfo bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "入住情况不存在");
        }
        String roomType = dto.getRoomType();
        String roomId = dto.getRoomId();
        String customerName = dto.getCustomerName();
        String orderTime = dto.getOrderTime();
        String note = dto.getNote();

        if (StringUtils.isBlank(roomType)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间类型");
        }
        bean.setRoomType(roomType);
        if (StringUtils.isBlank(roomId)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间号");
        }
        bean.setRoomId(roomId);
        if (StringUtils.isBlank(customerName)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写入住人员");
        }
        bean.setCustomerName(customerName);
        if (StringUtils.isBlank(orderTime)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请选择入住时间");
        }
        bean.setOrderTime(DateUtils.parse(orderTime, "yyyy-MM-dd"));
        if (StringUtils.isBlank(note)) {
            bean.setNote("");
        }else{
            bean.setNote(note);
        }
        mapper.updateById(bean);
        return bean;
    }

    @Override
    public ServiceInfo getById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        ServiceInfo bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "入住情况不存在");
        }
        bean.setStatus(-1);
        bean.setUt(DateUtils.getNow());
        mapper.updateById(bean);
    }

    @Override
    public int updateById(ServiceInfo bean) {
        return mapper.updateById(bean);
    }

    @Override
    public Long count() {
        ServiceInfo bean = new ServiceInfo();
        bean.setStatus(0);
        return mapper.selectCount(new QueryWrapper<>(bean));
    }

    @Override
    public List<DataGroupByDateDto> countByDay(Date beginDate, Date endDate) {
        return mapper.countByDay(beginDate, endDate);
    }
}